﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormMain
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim timerLhj As Timer '任务步骤
    Dim timerLhj_interval As Integer = 3000 '时间间隔
    Dim objClsSysParam As New clsSysParam
    Dim agv_website As String
    Dim objPgdMes As New clsPgdMes
    Dim objLhPgd As New clsLhPgd
    Dim objProdInfo As New clsProdInfo
    Dim objClsTaskStep As New clsTaskStep
    Dim objClsHWSetting As New clsHWSetting
    Dim objAutoNumber As New clsAutoNumber
    Dim objagvlog As New clsAgvLog
    Dim objPgdMesWorkReport As New clsPgdMesWorkReport
    Dim dt As DataTable
    Dim dgvSelectedIntoSysId As String = "-1"
    Dim dgvSelectedIntoLhPgdCode As String = "-1"
    Dim dgvSelectedIntoLhj As String = "-1"
    Dim dgvSelectedIntoSupportModel As String = "-1"
    Dim dgvSelectedIntoMesProjectName As String = "-1"
    Dim dthwlhj As DataTable
    Dim dgvSelectedOutSysId As String = "-1"
    Dim dttemp As New DataTable
    Dim rb_checked As String = "0"
    Dim httpHelper As New HttpHelper
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dttemp.Columns.Add("sys_id")
        dttemp.Columns.Add("lh_pgd_code")
        dttemp.Columns.Add("lhj")
        dttemp.Columns.Add("jp_type")
        dttemp.Columns.Add("prod_spec_id")
        dttemp.Columns.Add("support_model")
        dttemp.Columns.Add("mes_project_name")
        dttemp.Columns.Add("mes_project_code")
        Dim dd As String = String.Format("{0:D4}", 16)
        connectDB()
        If String.IsNullOrEmpty(txtOper.Text) Then
            txtOper.Enabled = True
            btnChangeOper.Text = "保存"
        Else
            txtOper.Enabled = False
            btnChangeOper.Text = "切换用户"
        End If
        If rb_checked.Equals("3") Then
            RadioButton1.Checked = True
        ElseIf rb_checked.Equals("4") Then
            RadioButton2.Checked = True
        ElseIf rb_checked.Equals("5") Then
            RadioButton3.Checked = True
        Else
            RadioButton1.Checked = True
        End If
        initAgvWebSite()
        initTimer()
        initInto()
        initOut()
    End Sub
    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_AGV_Lhj.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))
        txtOper.Text = GetIniFile("LOGIN", "user", "", g_szInitFileName)
        rb_checked = GetIniFile("LOGIN", "bz", "", g_szInitFileName).ToString()
        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub

    Private Sub initAgvWebSite()
        Dim dt As DataTable = objClsSysParam.selectAgvWebSite()
        If dt.Rows.Count = 1 Then
            agv_website = dt.Rows(0)("param_value").ToString()
        End If
    End Sub

    Private Sub initTimer()
        timerLhj = New Timer
        timerLhj.Interval = timerLhj_interval
        AddHandler timerLhj.Tick, AddressOf timerLhj_Tick
        timerLhj.Start()
    End Sub
    Private Sub timerLhj_Tick(sender As Object, e As EventArgs)
        Try
            initInto()
            initOut()
        Catch ex As Exception
            timerLhj.Stop()
            MsgBox("与服务器断开连接，请检查网络状态。")
            End 'Application.Restart()
        End Try

    End Sub

    Private Sub initInto()
        dttemp.Rows.Clear()
        Dim dtLHJ = objClsHWSetting.selectAllLHJ1()
        dt = objLhPgd.getLhPgd()
        For index = 0 To dt.Rows.Count - 1
            Dim lhjs As String = dt.Rows(index)("lhj").ToString()
            Dim lhj_list As String() = lhjs.Split(",")
            For inex = 0 To lhj_list.Length - 1
                Dim dr As DataRow() = dtLHJ.Select("equip_id = '" & lhj_list(inex) & "' and status = '0'")
                If dr.Length > 0 Then
                    dttemp.Rows.Add(dt.Rows(index)("sys_id").ToString(),
                                dt.Rows(index)("lh_pgd_code").ToString(),
                                lhj_list(inex),
                                dt.Rows(index)("jp_type").ToString(),
                                dt.Rows(index)("support_model").ToString(),
                                dt.Rows(index)("prod_spec_name").ToString(),
                                dt.Rows(index)("mes_project_name").ToString(),
                                dt.Rows(index)("mes_project_code").ToString())
                End If
            Next
        Next
        dgvInto.AutoGenerateColumns = False
        dgvInto.DataSource = dttemp
        For index = 0 To dgvInto.Rows.Count - 1
            dgvInto.Rows(index).Selected = False
        Next
        For index = 0 To dgvInto.Rows.Count - 1
            If dgvInto.Rows(index).Cells("Col_sys_id").Value.ToString() = dgvSelectedIntoSysId And
               dgvInto.Rows(index).Cells("Col_lh_pgd_code").Value.ToString() = dgvSelectedIntoLhPgdCode And
               dgvInto.Rows(index).Cells("Col_lhj").Value.ToString() = dgvSelectedIntoLhj And
               dgvInto.Rows(index).Cells("Col_support_model").Value.ToString() = dgvSelectedIntoSupportModel And
               dgvInto.Rows(index).Cells("Col_mes_project_name").Value.ToString() = dgvSelectedIntoMesProjectName Then
                dgvInto.Rows(index).Selected = True
                Exit For
            End If
        Next

    End Sub

    Private Sub initOut()
        dthwlhj = objClsHWSetting.selectAllLHJ()
        dgvOut.AutoGenerateColumns = False
        dgvOut.DataSource = dthwlhj
        For index = 0 To dgvOut.Rows.Count - 1
            dgvOut.Rows(index).Selected = False
        Next
        For index = 0 To dgvOut.Rows.Count - 1
            If dgvOut.Rows(index).Cells("Collhj").Value.ToString() = dgvSelectedOutSysId Then
                dgvOut.Rows(index).Selected = True
                Exit For
            End If
        Next
    End Sub

    Private Sub dgvInto_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvInto.MouseClick
        If dgvInto.Rows.Count > 0 Then
            dgvSelectedIntoSysId = dgvInto.Rows(dgvInto.CurrentRow.Index).Cells("Col_sys_id").Value.ToString()
            dgvSelectedIntoLhPgdCode = dgvInto.Rows(dgvInto.CurrentRow.Index).Cells("Col_lh_pgd_code").Value.ToString()
            dgvSelectedIntoLhj = dgvInto.Rows(dgvInto.CurrentRow.Index).Cells("Col_lhj").Value.ToString()
            dgvSelectedIntoSupportModel = dgvInto.Rows(dgvInto.CurrentRow.Index).Cells("Col_support_model").Value.ToString()
            dgvSelectedIntoMesProjectName = dgvInto.Rows(dgvInto.CurrentRow.Index).Cells("Col_mes_project_name").Value.ToString()
        End If
    End Sub

    Private Sub dgvOut_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvOut.MouseClick
        If dgvOut.Rows.Count > 0 Then
            dgvSelectedOutSysId = dgvOut.Rows(dgvOut.CurrentRow.Index).Cells("Collhj").Value.ToString()
            lblprod_id.Text = dgvOut.Rows(dgvOut.CurrentRow.Index).Cells("Colprod_id").Value.ToString()
        End If
    End Sub

    Private Sub getRbChecked()
        If RadioButton1.Checked Then
            rb_checked = "3"
        End If
        If RadioButton2.Checked Then
            rb_checked = "4"
        End If
        If RadioButton3.Checked Then
            rb_checked = "5"
        End If
    End Sub

    Private Sub btnInto_Click(sender As Object, e As EventArgs) Handles btnInto.Click
        If String.IsNullOrEmpty(txtOper.Text.Trim()) Then
            MsgBox("请输入用户")
            Exit Sub
        End If
        If String.IsNullOrEmpty(txtBarCodeInto.Text.Trim()) Then
            MsgBox("请扫描或输入标签号")
            Exit Sub
        End If
        If MsgBox("确认要进行入炉吗？", vbYesNo, "提示") = vbYes Then
            If dgvInto.SelectedRows.Count > 0 Then
                Dim dthwsetting As DataTable = objClsHWSetting.selectLHJbyID(dgvInto.SelectedRows(0).Cells("Col_lhj").Value.ToString())
                If dthwsetting.Rows.Count = 0 Then
                    'Dim prod_id As String = Now.ToString("yyyyMMdd") & "-" & objAutoNumber.getNextKeyNo("prod_code", "prodAutoNumber") '生产编号
                    'If String.IsNullOrEmpty(txt_prod_id.Text.Trim()) = False Then
                    Dim prod_id As String = txtBarCodeInto.Text.ToUpper().Trim()
                    Dim dtProdInfo As DataTable = objProdInfo.getProdInfoById(prod_id)
                    If dtProdInfo.Rows.Count = 1 Then
                        If String.IsNullOrEmpty(txtOper.Text.Trim()) = False Then
                            'Dim gyhHeader As String = getGYH_Header()
                            'Dim gyhBody = objAutoNumber.getNextKeyNo("gyh_code_" & gyhHeader, "gyhAutoNumber")
                            'Dim gyhBodyFormat As String = String.Format("{0:D6}", gyhBody)
                            Dim gyh_id As String = String.Empty 'gyhHeader & gyhBodyFormat '钢印号
                            Dim projectAutoNo As String = objAutoNumber.getNextKeyNoProject(dgvInto.SelectedRows(0).Cells("Col_mes_project_code").Value.ToString())
                            Dim project_no As String = String.Format("{0:D4}", projectAutoNo)
                            If ckAGV.Checked Then
                                Dim dts As DataTable = objClsTaskStep.SelectTaskStep1(dgvInto.SelectedRows(0).Cells("Col_lhj").Value.ToString(), "任务完成")
                                If dts.Rows.Count >= 1 Then
                                    If dts.Rows(0)("type").ToString().Equals("出入炉") Or dts.Rows(0)("type").ToString().Equals("入炉") Then
                                        Dim dts2 As DataTable = objClsTaskStep.SelectTaskStep2(dts.Rows(0)("sys_id").ToString())
                                        If dts2.Rows.Count = 1 Then
                                            Dim msg As String = setAgvNewTask(
                                                dts2.Rows(0)("sys_id").ToString(),
                                                                dts2.Rows(0)("prio").ToString(),
                                                                dts2.Rows(0)("pick").ToString(),
                                                                dts2.Rows(0)("drop").ToString(),
                                                                dts2.Rows(0)("agv").ToString())
                                            If msg.Equals("ok") Then
                                                'agv的操作日志
                                                Dim localip As String = GetIPAddr()
                                                objagvlog.add(localip, dts2.Rows(0)("agv").ToString(), dts2.Rows(0)("sys_id").ToString(), "入炉")
                                                getRbChecked()
                                                '更新status 状态为ok
                                                Dim rtnt As Integer = objClsTaskStep.updateTaskStepInto("已创建2", "已创建2",
                                                                                                    dts2.Rows(0)("sys_id").ToString(),
                                                                                                    dts2.Rows(0)("task_id").ToString(),
                                                                                                    prod_id, gyh_id, project_no, dgvInto.Rows(0).Cells("Col_lh_pgd_code").Value.ToString(),
                                                                                                    dgvInto.SelectedRows(0).Cells("Col_lhj").Value.ToString(),
                                                                                                    dgvInto.SelectedRows(0).Cells("Col_prod_spec_id").Value.ToString(),
                                                                                                    txt_jstzjl.Value, txtOper.Text.Trim(), rb_checked,
                                                                                                    dgvInto.Rows(0).Cells("Col_sys_id").Value.ToString(),
                                                                                                    dgvInto.Rows(0).Cells("Col_jp_type").Value.ToString())
                                                If rtnt <> -2 Then
                                                    'objAutoNumber.setNextKeyNo("prod_code", "prodAutoNumber")
                                                    'objAutoNumber.setNextKeyNo("gyh_code_" & gyhHeader, "gyhAutoNumber")
                                                    MsgBox("入炉成功")
                                                    'txt_prod_id.Text = String.Empty
                                                    txt_jstzjl.Value = 0.00
                                                Else
                                                    MsgBox("入炉失败")
                                                End If
                                            Else
                                                MsgBox("入炉失败：" & msg)
                                            End If
                                        Else
                                            MsgBox("未检索到任务2")
                                        End If
                                    Else
                                        MsgBox("当前操作类型为：" & dts.Rows(0)("type").ToString())
                                    End If
                                Else
                                    MsgBox("未检索到任务1")
                                End If
                            Else
                                getRbChecked()
                                Dim rtnt As Integer = objClsTaskStep.updateInto(prod_id, gyh_id, project_no, dgvInto.Rows(0).Cells("Col_lh_pgd_code").Value.ToString(),
                                                                            dgvInto.SelectedRows(0).Cells("Col_lhj").Value.ToString(),
                                                                            dgvInto.SelectedRows(0).Cells("Col_prod_spec_id").Value.ToString(),
                                                                            txt_jstzjl.Value, txtOper.Text.Trim(), rb_checked,
                                                                            dgvInto.Rows(0).Cells("Col_sys_id").Value.ToString(),
                                                                            dgvInto.Rows(0).Cells("Col_jp_type").Value.ToString())
                                If rtnt <> -2 Then
                                    'objAutoNumber.setNextKeyNo("prod_code", "prodAutoNumber")
                                    'objAutoNumber.setNextKeyNo("gyh_code_" & gyhHeader, "gyhAutoNumber")
                                    MsgBox("入炉成功")
                                    'txt_prod_id.Text = String.Empty
                                    txt_jstzjl.Value = 0.00
                                Else
                                    MsgBox("入炉失败")
                                End If
                            End If
                        Else
                            MsgBox("请点击切换用户，登录操作员")
                        End If
                    Else
                        MsgBox("上层法兰板编号已存在，请核查！")
                    End If
                    'Else
                    '    MsgBox("请输入上层法兰板编号")
                    'End If
                Else
                    MsgBox("此硫化机内有已入炉的支座，请先出炉")
                End If
            Else
                MsgBox("请选择需入炉的硫化机")
            End If
        End If
    End Sub

    Private Sub btnOut_Click(sender As Object, e As EventArgs) Handles btnOut.Click
        If String.IsNullOrEmpty(txtOper.Text.Trim()) Then
            MsgBox("请输入用户")
            Exit Sub
        End If
        If String.IsNullOrEmpty(txtBarCodeOut.Text.Trim()) Then
            MsgBox("请扫描或输入标签号")
            Exit Sub
        End If
        If txtBarCodeOut.Text.ToUpper().Trim().Equals(lblprod_id.Text.ToUpper.Trim()) = False Then
            MsgBox("标签号与入炉的标签号不一致，请核查！")
            Exit Sub
        End If
        If MsgBox("确认要进行出炉吗？", vbYesNo, "提示") = vbYes Then
            If dgvOut.SelectedRows.Count > 0 Then
                If lblprod_id.Text.Equals("-") = False Then
                    If String.IsNullOrEmpty(txtOper.Text.Trim()) = False Then
                        If ckAGV.Checked Then
                            Dim dts As DataTable = objClsTaskStep.SelectTaskStep1(dgvOut.SelectedRows(0).Cells("Collhj").Value.ToString(), "任务完成")
                            If dts.Rows.Count >= 1 Then
                                If dts.Rows(0)("type").ToString().Equals("出炉") Then
                                    Dim dts2 As DataTable = objClsTaskStep.SelectTaskStep2(dts.Rows(0)("sys_id").ToString())
                                    If dts2.Rows.Count = 1 Then
                                        Dim msg As String = setAgvNewTask(dts2.Rows(0)("sys_id").ToString(),
                                                      dts2.Rows(0)("prio").ToString(),
                                                      dts2.Rows(0)("pick").ToString(),
                                                      dts2.Rows(0)("drop").ToString(),
                                                      dts2.Rows(0)("agv").ToString())
                                        If msg.Equals("ok") Then
                                            'agv的操作日志
                                            Dim localip As String = GetIPAddr()
                                            objagvlog.add(localip, dts2.Rows(0)("agv").ToString(), dts2.Rows(0)("sys_id").ToString(), "出炉")
                                            getRbChecked()
                                            '更新status 状态为ok
                                            Dim rtnt As Integer = objClsTaskStep.updateTaskStepOut("已创建2", "已创建2",
                                                                                            dts2.Rows(0)("sys_id").ToString(), dts2.Rows(0)("task_id").ToString(),
                                                                                            dts2.Rows(0)("prod_id").ToString().ToUpper().Trim(), txt_yjzl.Value,
                                                                                            txtOper.Text.Trim(), rb_checked, dgvOut.SelectedRows(0).Cells("Collhj").Value.ToString())
                                            If rtnt <> -2 Then
                                                makePgdMesBg()
                                                lblprod_id.Text = "-"
                                                txt_yjzl.Value = 0.00
                                            Else
                                                MsgBox("出炉失败")
                                            End If
                                        Else
                                            MsgBox("出炉失败：" & msg)
                                        End If
                                    Else
                                        MsgBox("未检索到任务2")
                                    End If
                                ElseIf dts.Rows(0)("type").ToString().Equals("出入炉") Then
                                    getRbChecked()
                                    Dim rtnt As Integer = objClsTaskStep.updateOut(lblprod_id.Text.ToUpper().Trim(),
                                                                        dgvOut.SelectedRows(0).Cells("Collhj").Value.ToString(),
                                                                        txt_yjzl.Value, txtOper.Text.Trim(), rb_checked)
                                    If rtnt <> -2 Then
                                        makePgdMesBg()
                                        lblprod_id.Text = "-"
                                        txt_yjzl.Value = 0.00
                                    Else
                                        MsgBox("出炉失败")
                                    End If
                                Else
                                    MsgBox("当前操作类型为：" & dts.Rows(0)("type").ToString())
                                End If
                            Else
                                MsgBox("未检索到任务1")
                            End If
                        Else
                            Dim rtnt As Integer = objClsTaskStep.updateOut(lblprod_id.Text.ToUpper().Trim(),
                                                                        dgvOut.SelectedRows(0).Cells("Collhj").Value.ToString(),
                                                                        txt_yjzl.Value, txtOper.Text.Trim(), rb_checked)
                            If rtnt <> -2 Then
                                makePgdMesBg()
                                lblprod_id.Text = "-"
                                txt_yjzl.Value = 0.00
                            Else
                                MsgBox("出炉失败")
                            End If
                        End If
                    Else
                        MsgBox("请点击切换用户，登录操作员")
                    End If
                Else
                    MsgBox("请点击选择出炉硫化机")
                End If
            Else
                MsgBox("请选择出炉硫化机")
            End If
        End If

    End Sub

    Private Sub makePgdMesBg()
        If lblprod_id.Text.Equals("-") = False Then
            Dim dtpii As DataTable = objProdInfo.getProdInfoById(lblprod_id.Text.Trim())
            Dim dtlhpgdid As DataTable = objLhPgd.getListById(dtpii.Rows(0)("b_pgd_lh_details_sys_id").ToString())
            Dim dtpgdmes As DataTable = objPgdMes.getList(dtlhpgdid.Rows(0)("b_pgd_mes_sys_id").ToString())
            Dim b_SysId As String = dtpgdmes.Rows(0)("sys_id").ToString()
            Dim b_PgdMesProjectCode As String = dtpgdmes.Rows(0)("mes_project_code").ToString()
            Dim b_PgdMes_gx As String = dtpgdmes.Rows(0)("type_value").ToString()

            Dim dtPgdMesWorkReport As DataTable = objPgdMesWorkReport.getListByPgdmesSysId("ok", b_SysId)
            If dtPgdMesWorkReport.Rows.Count > 0 Then
                If MsgBox("确认要进行合格报工吗？", vbYesNo, "提示") = vbYes Then
                    'If MsgBox("此生产记录已报工过，确认要进行再次合格报工吗？", vbYesNo, "提示") = vbYes Then
                    Dim mes_bg_rtn As Integer = mesbg(b_SysId, 1.ToString(), b_PgdMes_gx, b_PgdMesProjectCode)
                    If mes_bg_rtn <> -2 Then
                        MsgBox("出炉成功，报工成功", vbYes, "提示")
                    Else
                        MsgBox("出炉成功，报工失败", vbYes, "提示")
                    End If
                End If
            Else
                If MsgBox("确认要进行合格报工吗？", vbYesNo, "提示") = vbYes Then
                    Dim mes_bg_rtn As Integer = mesbg(b_SysId, 1.ToString(), b_PgdMes_gx, b_PgdMesProjectCode)
                    If mes_bg_rtn <> -2 Then
                        MsgBox("出炉成功，报工成功", vbYes, "提示")
                    Else
                        MsgBox("出炉成功，报工失败", vbYes, "提示")
                    End If
                End If
            End If
        End If
    End Sub

    Private Function setAgvNewTask(id As String, prio As String, pick As String, drop As String, agv As String) As String
        Dim msg As String
        If String.IsNullOrEmpty(agv_website) = False Then
            Dim url As String = agv_website & "AGVAPI?do=NewTask&id=" & id & "&prio=" & prio & "&pick=" & pick & "&drop=" & drop & "&agv=" & agv
            Dim httphelper As HttpHelper = New HttpHelper()
            Dim rtn As String = httphelper.HttpGet(url, "")
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
            Dim code As String = jo("code").ToString() '输出0
            msg = jo("msg").ToString() '输出ok
        Else
            msg = "请联系工作人员，配置agv网址"
        End If
        Return msg
    End Function

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

    Private Sub btnChangeOper_Click(sender As Object, e As EventArgs) Handles btnChangeOper.Click
        If btnChangeOper.Text.Equals("切换用户") Then
            txtOper.Enabled = True
            btnChangeOper.Text = "保存"
        Else
            If btnChangeOper.Text.Equals("保存") Then
                If String.IsNullOrEmpty(txtOper.Text.Trim()) = False Then
                    PutIniFile("LOGIN", "user", txtOper.Text.Trim(), g_szInitFileName)
                    txtOper.Enabled = False
                    btnChangeOper.Text = "切换用户"
                Else
                    MsgBox("请输入操作员账号")
                End If
            End If
        End If

    End Sub

    Private Sub txt_MouseDown(sender As Object, e As MouseEventArgs) Handles txt_jstzjl.MouseDown, txt_yjzl.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub ucdigitalkeyboard1_leave(sender As Object, e As EventArgs) Handles UcDigitalKeyboard1.Leave
        UcDigitalKeyboard1.Dispose()
    End Sub

    Private Sub btnLhjAdd_Click(sender As Object, e As EventArgs) Handles btnLhjAdd.Click
        FormPlan.ShowDialog()
    End Sub

    Private Sub btnRepair_Click(sender As Object, e As EventArgs) Handles btnRepair.Click
        If dgvInto.SelectedRows.Count > 0 Then
            If MsgBox("选中的此台硫化机确认要进行维修吗？", vbYesNo, "提示") = vbYes Then
                Dim rtn As Integer = objClsHWSetting.SetLHJstatus("5", dgvInto.SelectedRows(0).Cells("Col_lhj").Value.ToString())
                If rtn <> -2 Then
                    MsgBox("操作成功")
                Else
                    MsgBox("操作失败")
                End If
            End If
        Else
            MsgBox("请选中要维修的硫化机")
        End If
    End Sub

    Private Sub lblQuit_Click(sender As Object, e As EventArgs) Handles lblQuit.Click
        PutIniFile("LOGIN", "user", "", g_szInitFileName)
        txtOper.Text = GetIniFile("LOGIN", "user", "", g_szInitFileName)
    End Sub

    Private Sub RadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
        getRbChecked()
        PutIniFile("LOGIN", "bz", rb_checked, g_szInitFileName)
    End Sub

    Private Sub btnOutOnly_Click(sender As Object, e As EventArgs) Handles btnOutOnly.Click
        If String.IsNullOrEmpty(txtOper.Text.Trim()) Then
            MsgBox("请输入用户")
            Exit Sub
        End If
        If MsgBox("确认要进行只出炉吗？", vbYesNo, "提示") = vbYes Then
            If dgvOut.SelectedRows.Count > 0 Then
                If lblprod_id.Text.Equals("-") = False Then
                    If String.IsNullOrEmpty(txtOper.Text.Trim()) = False Then
                        If ckAGV.Checked Then
                            Dim dts As DataTable = objClsTaskStep.SelectTaskStep1(dgvOut.SelectedRows(0).Cells("Collhj").Value.ToString(), "任务完成")
                            If dts.Rows.Count >= 1 Then
                                Dim dts2 As DataTable = objClsTaskStep.SelectTaskStep2(dts.Rows(0)("sys_id").ToString())
                                If dts2.Rows.Count = 1 Then
                                    Dim msg As String = setAgvNewTask(dts2.Rows(0)("sys_id").ToString(),
                                                                          dts2.Rows(0)("prio").ToString(),
                                                                          dts2.Rows(0)("pick").ToString(),
                                                                          dts2.Rows(0)("drop").ToString(),
                                                                          dts2.Rows(0)("agv").ToString())
                                    If msg.Equals("ok") Then
                                        'agv的操作日志
                                        Dim localip As String = GetIPAddr()
                                        objagvlog.add(localip, dts2.Rows(0)("agv").ToString(), dts2.Rows(0)("sys_id").ToString(), "出炉")
                                        getRbChecked()
                                        '更新status 状态为ok
                                        Dim rtnt As Integer = objClsTaskStep.updateTaskStepOut("已创建2", "已创建2",
                                                                                                dts2.Rows(0)("sys_id").ToString(), dts2.Rows(0)("task_id").ToString(),
                                                                                                dts2.Rows(0)("prod_id").ToString(), txt_yjzl.Value,
                                                                                                txtOper.Text.Trim(), rb_checked, dgvOut.SelectedRows(0).Cells("Collhj").Value.ToString())
                                        If rtnt <> -2 Then
                                            makePgdMesBg()
                                            lblprod_id.Text = "-"
                                            txt_yjzl.Value = 0.00
                                        Else
                                            MsgBox("只出炉失败")
                                        End If
                                    Else
                                        MsgBox("出炉失败：" & msg)
                                    End If
                                Else
                                    MsgBox("未检索到任务2")
                                End If
                            Else
                                MsgBox("未检索到任务1")
                            End If
                        Else
                            Dim rtnt As Integer = objClsTaskStep.updateOut(lblprod_id.Text,
                                                                        dgvOut.SelectedRows(0).Cells("Collhj").Value.ToString(),
                                                                        txt_yjzl.Value, txtOper.Text.Trim(), rb_checked)
                            If rtnt <> -2 Then
                                makePgdMesBg()
                                lblprod_id.Text = "-"
                                txt_yjzl.Value = 0.00
                            Else
                                MsgBox("只出炉失败")
                            End If
                        End If
                    Else
                        MsgBox("请点击切换用户，登录操作员")
                    End If
                Else
                    MsgBox("请点击选择出炉硫化机")
                End If
            Else
                MsgBox("请选择出炉硫化机")
            End If
        End If

    End Sub

    Private Function mesbg(PgdMesSysId As String, oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As Integer
        Dim rtn_int As Integer = -2
        rtn_int = objPgdMesWorkReport.insert("ok", PgdMesSysId, oper_num, txtOper.Text.Trim())
        'Dim params As String = "{"
        'params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        'params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        'params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        'params &= """productorname"":""" & productorname & """," '报工产品编码
        'params &= """procedurename"":""" & "硫化" & """," '报工工序
        'params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        'params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        'params &= "}"
        'Dim rtn As String = HttpHelper.HttpPostWebService("dispatchingOrderBG", params)
        'Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        'Dim code As String = jo("code").ToString() '输出0
        'Dim msg As String = jo("msg").ToString() '输出操作成功
        'If code.Equals("0") And msg.Equals("操作成功") Then
        '    rtn_int = objPgdMesWorkReport.insert("ok", PgdMesSysId, oper_num, txtOper.Text.Trim())
        'End If
        Return rtn_int
    End Function

    Private Sub btnLhCurrent_Click(sender As Object, e As EventArgs) Handles btnLhCurrent.Click
        FormCurrent.ShowDialog()
    End Sub

    Private Sub btnMesWorkReportList_Click(sender As Object, e As EventArgs) Handles btnMesWorkReportList.Click
        Dim formMesWorkReportList As New FormMesWorkReportList()
        formMesWorkReportList.ShowDialog(Me)
    End Sub

    Private Sub btnWarehouseYZZH_Click(sender As Object, e As EventArgs) Handles btnWarehouseYZZH.Click
        Dim formWarehouseYZZH As New FormWarehouseYZZH()
        formWarehouseYZZH.ShowDialog(Me)
    End Sub
End Class